/**
 * 
 */
package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.request.domain.Request;
import org.ediba.siri.core.request.domain.RequestTake;
import org.ediba.siri.core.request.service.RequestService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.ResponseBody;

public class RequestTakeController extends Controller {

	private RequestService requestService;
	public void setRequestService(RequestService requestService) {this.requestService = requestService;}
	
	@ResponseBody
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse response, RequestTakeCommand requestTakeCommand){

		String name = null;
		try {name = requestTakeCommand.getData().getString("name");
		} catch (JSONException e) {e.printStackTrace();}
		
		RequestTake requestTake =  this.requestService.takeCreate(name, false);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", requestTake);
		_result.put("success", true);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, RequestTakeCommand command){
		List<RequestTake> requestTakes = null;
		
		if (command.getIdRequest()!=null){
			Request req = this.requestService.retrieve(command.getIdRequest());
			requestTakes = new ArrayList<RequestTake>(req.getRequesttakes());
		}else
			requestTakes = this.requestService.takeRetrieve();
		
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", requestTakes);
		_result.put("total", requestTakes.size());
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, RequestTakeCommand command){
		
		Integer id = null;
		String name = null;
		try {
			id = command.getData().getInt("id");
			name = command.getData().getString("name");
		} catch (JSONException e) {e.printStackTrace();}
		
		RequestTake requestTake = this.requestService.takeRetrieve(id);
		if (name != null)
			requestTake.setName(name);
		requestTake.setMessage(false);
		
		requestTake = this.requestService.takeUpdate(requestTake);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", requestTake);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, RequestTakeCommand requestTakeCommand){

		Integer id = null;
		try {id = requestTakeCommand.getData().getInt("id");}
		catch (JSONException e) {e.printStackTrace();}
		
		boolean _success = this.requestService.takeDelete(id);
		
		Map<String,Object> _result = new HashMap<String,Object>(1);
		_result.put("success", _success);
		return _result;
	}
}
